// pages/mine/mycol/mycol.js
let { AJAX, productKind, timeTranfer,imgUrl} = require("../../../utils/util.js")
const App = getApp()
Page({

   /** 
    * 页面的初始数据
    */ 
   data: {      
      dataList: [],
      page: 1,
      total: 0,
      floorstatus: false,
      text: '加载更多',
      showThis: false,
      showIcon: false,
      isLoading: false,
     imgUrl: imgUrl     
   },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function(options) {
      this.setData({
         navH: App.globalData.navHeight
      })
      if (!wx.getStorageSync("ACCESSTOKEN")) {
         App.userInfoReadyCallback = res => {
            wx.setStorageSync('ACCESSTOKEN', res);
            this.requestList()
         }
         return
      }
      this.requestList()
   },

   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function() {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function() {
   
   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function() {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function() {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function() {
      this.setData({
         page: 1
      })
      this.requestList()
      // 显示顶部刷新图标
      wx.showNavigationBarLoading();
   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function() {
      let vm = this
      console.log(vm.data.total)
      console.log(vm.data.dataList.length)
      if(vm.data.total > vm.data.dataList.length) {
         console.log(vm.data.page)   
         vm.setData({
            page:vm.data.page + 1 
         })
         vm.requestList(true)
      }else {         
         this.setData({
            text: '我是有底线的',
            showIcon: false,
            showThis: true
         })
      }
   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function() {

   },
   navBack() {
      wx.navigateBack()
   },
   requestList(str) {
      let vm = this
      AJAX('member-collection/my-collection',"GET", {
         page: vm.data.page
      }).then(res => {
         // 隐藏导航栏加载框
         wx.hideNavigationBarLoading();
         // 停止下拉动作
         wx.stopPullDownRefresh();
         let _data = []
         if (str) {
            _data = vm.data.dataList
         }
            
            res.data.forEach(function(item) {
               let _obj = {}
               _obj = item.product
              
               _obj.productSchedules = item.productSchedule
               _obj.product_kind = productKind[_obj.product_kind || 1]
               if (_obj.theme_name) {
                  _obj.theme_name = _obj.theme_name.split('|')
               }
               _obj.begin_time = timeTranfer(_obj.begin_time, 1)
               _obj.end_time = timeTranfer(_obj.end_time, 1)
               _data.push(item.product)
            })
         
         
         vm.setData({          
            total: res.header["X-Pagination-Total-Count"],
            dataList: _data
         })
         
      })
   },
   toInfo(e) {
      console.log(e)
      let _id = e.currentTarget.dataset.id
      let _delete = e.currentTarget.dataset.delete 
      let _status = e.currentTarget.dataset.status     
      if (_delete == 0 && _status !=2){
         wx.navigateTo({
            url: '/pages/product/productinfo/productinfo?id=' + _id,
         })
      } 
     
   },
   tabActive(e) {
      let _type = e.currentTarget.dataset.type
      this.setData({
         activeNum: _type
      })
      this.requestList()
   }
})